<?php
/** 初始化项目 */
require '../../app/app.php';
/** 初始化数据库 */
use app\util\Mysql;
$database = Mysql::init();

session_start();
$username = $_SESSION['username'];
if (!$username) {
    return_json(0,'暂未登录，无权限');
}

$blacklist = isset($_POST['blacklist']) ? $_POST['blacklist'] : '';
$whitelist = isset($_POST['whitelist']) ? $_POST['whitelist'] : '';

// 按行拆分黑名单和白名单内容
$blacklistArray = explode(PHP_EOL, $blacklist);
$whitelistArray = explode(PHP_EOL, $whitelist);

// 将数组转换为以英文逗号分隔的字符串
$blacklistString = implode(",", $blacklistArray);
$whitelistString = implode(",", $whitelistArray);

$database->update("sys_config",['value'=>$blacklistString],['name'=>'black_ips']);
$database->update("sys_config",['value'=>$whitelistString],['name'=>'white_ips']);

require_once 'getip.php';
$client_ip = getClientIP();
$timestamp = time();
$database->insert("secure_info",["user"=>$username,"ip"=>$client_ip,"times"=>$timestamp,"content"=>"修改了IP防护列表。"]);

return return_json(1,'Success');
function return_json($code = 1,$msg = '',$data = [])
{
    echo json_encode([
        'code' => $code,
        'msg' => $msg,
        'data' => $data
    ]);
}
